<template>
  <view class="container">
    <form @submit="submitOrder">
      <!-- 基本信息 -->
      <view class="section">
        <view class="section-title">基本信息</view>

        <view class="form-group">
          <label>工单标题</label>
          <input
            type="text"
            v-model="order.title"
            placeholder="请输入工单标题"
            class="input-placeholder"
          />
        </view>

        <view class="form-group">
          <label>工单类型</label>
          <picker
            mode="selector"
            :range="types"
            :value="order.typeIndex"
            @change="onTypeChange"
          >
            <view class="picker">
              {{ order.type || '请选择工单类型' }}
            </view>
          </picker>
        </view>

        <view class="form-group">
          <label>工单类别</label>
          <input
            type="text"
            v-model="order.category"
            placeholder="请输入工单类别"
            class="input-placeholder"
          />
        </view>
      </view>

      <!-- 请求者信息 -->
      <view class="section">
        <view class="section-title">请求者信息</view>

        <view class="form-group">
          <label>请求者</label>
          <input
            type="text"
            v-model="order.requestUser"
            placeholder="请输入请求者姓名"
            class="input-placeholder"
          />
        </view>

        <view class="form-group">
          <label>身份证号</label>
          <input
            type="text"
            v-model="order.requestIdCard"
            placeholder="请输入身份证号"
            class="input-placeholder"
          />
        </view>
      </view>

      <!-- 工单详情 -->
      <view class="section">
        <view class="section-title">工单详情</view>

        <view class="form-group">
          <label>逻辑卡号</label>
          <input
            type="text"
            v-model="order.logicalCardNumber"
            placeholder="请输入逻辑卡号"
            class="input-placeholder"
          />
        </view>

        <view class="form-group">
          <label>案件号</label>
          <input
            type="text"
            v-model="order.caseNumber"
            placeholder="请输入案件号"
            class="input-placeholder"
          />
        </view>

        <view class="form-group">
          <label>工单内容</label>
          <textarea
            v-model="order.content"
            placeholder="请输入工单内容"
            class="input-placeholder"
          />
        </view>

        <view class="form-group">
          <label>银行处理建议</label>
          <input
            type="text"
            v-model="order.bankProcessingAdvice"
            placeholder="请输入银行处理建议"
            class="input-placeholder"
          />
        </view>

        <view class="form-group">
          <label>工单预警级别</label>
          <picker
            mode="selector"
            :range="alertLevels"
            :value="order.alertLevelIndex"
            @change="onAlertLevelChange"
          >
            <view class="picker">
              {{ order.alertLevelDisplay || '请选择预警级别' }}
            </view>
          </picker>
        </view>

        <view class="form-group">
          <label>是否为监管投诉</label>
          <switch
            :checked="order.isRegulatoryComplaint"
            @change="onRegulatoryComplaintChange"
          />
        </view>
      </view>

      <!-- 其他信息 -->
      <view class="section">
        <view class="section-title">其他信息</view>

        <view class="form-group">
          <label>地区</label>
          <input
            type="text"
            v-model="order.region"
            placeholder="请输入地区"
            class="input-placeholder"
          />
        </view>

        <view class="form-group">
          <label>经办人</label>
          <input
            type="text"
            v-model="order.handler"
            placeholder="请输入经办人"
            class="input-placeholder"
          />
        </view>

        <view class="form-group">
          <label>经理</label>
          <input
            type="text"
            v-model="order.manager"
            placeholder="请输入经理"
            class="input-placeholder"
          />
        </view>

        <view class="form-group">
          <label>组别</label>
          <input
            type="text"
            v-model="order.teamCategory"
            placeholder="请输入组别"
            class="input-placeholder"
          />
        </view>

        <view class="form-group">
          <label>组员</label>
          <input
            type="text"
            v-model="order.teamMember"
            placeholder="请输入组员"
            class="input-placeholder"
          />
        </view>

        <view class="form-group">
          <label>委案批次</label>
          <input
            type="text"
            v-model="order.caseBatch"
            placeholder="请输入委案批次"
            class="input-placeholder"
          />
        </view>

        <view class="form-group">
          <label>手别</label>
          <input
            type="text"
            v-model="order.manualType"
            placeholder="请输入手别"
            class="input-placeholder"
          />
        </view>

        <view class="form-group">
          <label>下一级处理者</label>
          <input
            type="text"
            v-model="order.targetUser"
            placeholder="请输入下一级处理者"
            class="input-placeholder"
          />
        </view>

        <view class="form-group">
          <label>目标处理机构</label>
          <input
            type="text"
            v-model="order.targetOrganization"
            placeholder="请输入目标处理机构"
            class="input-placeholder"
          />
        </view>

        <view class="form-group">
          <label>工单预期完成时间</label>
          <input
            type="date"
            v-model="order.expectedEndTime"
            placeholder="请选择预期完成时间"
            class="input-placeholder"
          />
        </view>
      </view>

      <!-- 附件上传 -->
      <view class="section">
        <view class="section-title">附件上传</view>
        <view class="form-group">
          <label>上传附件</label>
          <uploader v-model="order.annex" :max-count="5" />
        </view>
        <view class="form-group" v-if="order.type === '材料处理工单'">
          <label>材料上传</label>
          <uploader v-model="order.material" :max-count="5" />
        </view>
      </view>

      <button class="submit-button" formType="submit">提交</button>
    </form>
  </view>
</template>

<script setup>
import { ref } from 'vue';

const types = ref(['普通工单', '材料处理工单','重述工单']);
const alertLevels = ref(['低级别预警', '中级别预警', '高级别预警', '紧急预警']);

const order = ref({
  title: '',
  type: '',
  typeIndex: 0,
  category: '',
  requestUser: '',
  requestIdCard: '',
  logicalCardNumber: '',
  caseNumber: '',
  content: '',
  bankProcessingAdvice: '',
  alertLevel: null,
  alertLevelIndex: 0,
  alertLevelDisplay: '',
  isRegulatoryComplaint: false,
  region: '',
  handler: '',
  manager: '',
  teamCategory: '',
  teamMember: '',
  caseBatch: '',
  manualType: '',
  targetUser: '',
  targetOrganization: '',
  expectedEndTime: '',
  annex: [],
  material: []
});

// 工单类型选择
const onTypeChange = (e) => {
  order.value.typeIndex = e.detail.value;
  order.value.type = types.value[e.detail.value];
};

// 预警级别选择
const onAlertLevelChange = (e) => {
  order.value.alertLevelIndex = e.detail.value;
  order.value.alertLevel = e.detail.value + 1;
  order.value.alertLevelDisplay = alertLevels.value[e.detail.value];
};

// 监管投诉开关
const onRegulatoryComplaintChange = (e) => {
  order.value.isRegulatoryComplaint = e.detail.value;
};

// 提交表单 
// 逻辑检验
const submitOrder = (e) => {
  e.preventDefault();
  if (!order.value.title) {
    uni.showToast({
      title: '请输入工单标题',
      icon: 'none'
    });
    return;
  }
  

  console.log('提交数据：', order.value);
  uni.showToast({
    title: '工单提交成功',
    icon: 'success'
  });
  uni.navigateBack();
};

const goBack = () => {
  uni.navigateBack();
};
</script>

<style lang="scss" scoped>
view {
  font-family: Arial, sans-serif;
}
	
.input-placeholder {
  font-size: 24rpx;
  color: #999999;
  line-height: 80rpx;
}

.container {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding: 20rpx;
  box-sizing: border-box;

  .header {
    display: flex;
    align-items: center;
    padding: 20rpx 0;
    margin-bottom: 20rpx;
    border-bottom: 2rpx solid #e0e0e0;

    .back-button {
      background-color: transparent;
      border: none;
      font-size: 28rpx;
      color: #007aff;
      margin-right: 20rpx;
      cursor: pointer;
      &:hover {
        opacity: 0.8;
      }
    }

    .title {
      font-size: 32rpx;
      font-weight: bold;
      color: #333;
    }
  }

  .section {
    background-color: #ffffff;
    border-radius: 16rpx;
    box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.05);
    padding: 20rpx;
    margin-bottom: 20rpx;

    .section-title {
      font-size: 32rpx;
      font-weight: bold;
      color: #333;
      margin-bottom: 20rpx;
      border-left: 6rpx solid #007aff;
      padding-left: 10rpx;
    }

    .form-group {
      margin-bottom: 20rpx;

      label {
        font-size: 28rpx;
        color: #333;
        margin-bottom: 10rpx;
        display: block;
      }

      input,
      textarea,
      .picker {
        width: 100%;
        font-size: 28rpx;
        padding: 0 20rpx;       
        height: 80rpx;         
        line-height: 80rpx;    
        border: 2rpx solid #e0e0e0;
        border-radius: 8rpx;
        box-sizing: border-box;
        background-color: #fdfdfd;

        &:focus {
          border-color: #c0c0c0;
        }
      }

      textarea {
        min-height: 160rpx;    
        line-height: normal;   
        padding: 20rpx;       
      }
    }
  }

  .submit-button {
    width: 100%;
    height: 64rpx;
    font-size: 28rpx;
    color: #ffffff;
    background-color: #007aff;
    border: none;
    border-radius: 8rpx;
    display: block;
    text-align: center;
    line-height: 64rpx;
    margin-top: 20rpx;
    cursor: pointer;
    box-shadow: 0 4rpx 8rpx rgba(0, 0, 0, 0.1);

    &:hover {
      background-color: #005bb5;
    }
  }
}
</style>
